//package com.whxd.framework.security;
//
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.Set;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.dao.DataAccessException;
//import org.springframework.security.authentication.DisabledException;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.GrantedAuthorityImpl;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//
//import com.google.common.collect.Sets;
//import com.whxd.system.user.entity.User;
//import com.whxd.system.user.service.UserService;
//
//public class MyUserDetailsServiceImpl implements UserDetailsService {
//
//	private final static Logger log = LoggerFactory
//			.getLogger(MyUserDetailsServiceImpl.class);
//
//	@Autowired
//	private UserService userService;
//
//	public UserDetails loadUserByUsername(String username)
//			throws UsernameNotFoundException, DataAccessException {
//		User user = userService.findByUsername(username);
//
//		if (user == null) {
//			throw new UsernameNotFoundException("用户" + username + " 不存在");
//		}
//		if (user.getEnabled() == false) {
//			throw new DisabledException("用户已被禁用");
//		}
//		// userService.updateLoginTime(user.getId());
//		//
//		List<String> authorities = userService
//				.findAuthorityNameByUsername(username);
//		authorities.add("IS_LOGINED");// 手工添加已经登录标识
//
//		Set<GrantedAuthority> grantedAuths = obtainGrantedAuthorities(authorities);
//
//		// addCustomAuthorities(user.getLoginName(), dbAuths);
//
//		boolean enabled = user.getEnabled();
//		boolean accountNonExpired = true;
//		boolean credentialsNonExpired = true;
//		boolean accountNonLocked = true;
//
//		// List<Organization> orgList = new
//		// ArrayList<Organization>(user.getOrganizations());
//
//		FrameworkUserDetails userDetails = new FrameworkUserDetails(
//				user.getUsername(), user.getPassword(), enabled,
//				accountNonExpired, credentialsNonExpired, accountNonLocked,
//				grantedAuths);
//		// 加入登录时间信息和用户角色
//		userDetails.setLoginTime(new Date());
//		userDetails.setId(user.getId());
//		userDetails.setFullname(user.getFullname());
//		userDetails.setUsername(user.getUsername());
//		// userDetails.setRoleList(roleList);
//		return userDetails;
//	}
//
//	/**
//	 * 获得用户的角色权限.
//	 */
//	private Set<GrantedAuthority> obtainGrantedAuthorities(List<String> roleList) {
//		Set<GrantedAuthority> authSet = Sets.newHashSet();
//		if (roleList != null) {
//			for (int i = 0; i < roleList.size(); i++) {
//				String roleName = roleList.get(i);
//				authSet.add(new GrantedAuthorityImpl(roleName));
//			}
//		}
//		return authSet;
//	}
//
//	// public void setUserService(UserService userService) {
//	// this.userService = userService;
//	// }
//
//}